// JavaScript Document

$(document).ready(function() 
{
 
	// Check Firstname
	$("#firstname").change(function() 
	{ 
		if($(this).val() == '') 
		{
			//Blank or not
			$("#showMsgFN").html('<font id="error">Cannot be blank...</font>'); 
		}
		else
		{	//Validate special characters
			var FN = $(this).val();
			if(!(/^[A-Za-z]{2,15}$/.test(FN))) 
			{
				$("#showMsgFN").html('<font id="error">Just can be [A-Z a-z and _ ] only</font>');
			}
			else
			{
				$("#showMsgFN").html("<img src='images/tick.png' width='20px' height='20px' >");
			}
		}
	});//End Check Firstname
	
	// Check Lastname
	$("#lastname").change(function() 
	{ 
		if($(this).val() == '') 
		{
			//Blank or not
			$("#showMsgLN").html('<font id="error">Cannot be blank...</font>'); 
		}
		else
		{	//Validate special characters
			if(!(/^[A-Za-z]{2,15}$/.test($(this).val()))) 
			{
				$("#showMsgLN").html('<font id="error">Just can be [A-Z a-z and _ ] only</font>');
			}
			else
			{
				$("#showMsgLN").html("<img src='images/tick.png' width='20px' height='20px' >");
			}
		}
	});//End Check Lastname
	
	//Check email existed	
	$("#email").change(function() 
	{ 	
		if($(this).val() == '')
		{
			$("#showMsgEmail").html('<font id="error">Cannot be blank...</font>'); 
		}
		else if(!(/^[A-Za-z0-9\.\-]+\@\w+\.\w+(\.\w+)?$/.test($(this).val())))
		{
			$("#showMsgEmail").html('<font id="error">Invalid email type, try again...!!!</font>'); 
		}
		else
		{
			var data = {Email: $(this).val()};
			
			$.ajax
			({
				type: "POST",               	// Phương thức gởi đi
				url: "registercheckEmail.php",  // File xử lý dữ liệu được gởi
				data: data,            			// Dữ liệu gởi đến cho url 
				success: function(result) 		// Hàm chạy khi dữ liệu gởi thành công
				{ 								
					$("#showMsgEmail").html(result);
				}
			});
		}
	
	});//End Check email existed
	
	// Check Length Password
	$("#password").change(function() 
	{ 
		if($(this).val() == '') 
		{
			//Blank or not
			$("#showMsgPass").html('<font id="error">Cannot be blank...</font>'); 
		}
		else
		{	//Check length
			if(($(this).val().length) < 9) 
			{
				$("#showMsgPass").html('<font id="error">At least 9 characters</font>');
			}
			else
			{
				$("#showMsgPass").html("<img src='images/tick.png' width='20px' height='20px' >");
			}
		}
	});//End Length Password
	
	
	// Check Password Match
	$("#repassword").change(function() 
	{ 
		if($(this).val() == '') 
		{
			//Blank or not
			$("#showMsgRePass").html('<font id="error">Cannot be blank...</font>'); 
		}
		if(($(this).val().length) < 9)
		{
			$("#showMsgRePass").html('<font id="error">At least 9 characters</font>');
		}
		else
		{	//Validate pass matched
			if(!($(this).val() == $("#password").val())) 
			{
				//$("#showMsgPass").html('<font id="error">Password are not matched</font>');
				$("#showMsgRePass").html('<font id="error">Password are not matched</font>');
			}
			else
			{
				//$("#showMsgPass").html("<img src='images/tick.png' width='20px' height='20px' >");
				$("#showMsgRePass").html("<img src='images/tick.png' width='20px' height='20px' >");

			}
		}
	});//End Check Password Match
	
	// Check ID length
	$("#idcard").change(function() 
	{ 	//Validate special characters
		if($(this).val() == '') 
		{
			//Blank or not
			$("#showMsgIDcard").html('<font id="error">Cannot be blank...</font>'); 
		}
		else
		{	//Validate special characters
			if(!(/^[0]{1}[0-9]{8,8}$/.test($(this).val()))) 
			{
				$("#showMsgIDcard").html('<font id="error">Invalid ID No.</font>');
			}
			else
			{
				$("#showMsgIDcard").html("<img src='images/tick.png' width='20px' height='20px' >");
			}
		}
	});//End check ID length

});//End ready function
